/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamankodeglsupplier;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sia.common.ClassUtility;

/**
 *
 * @author hendro
 */
public class PerekamanKodeGLSupplierDAOImpl implements PerekamanKodeGLSupplierDAO {

    public PerekamanKodeGLSupplierDAOImpl() {
        ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
    }

    public List<PerekamanKodeGLSupplier> list() throws Exception {
        List<PerekamanKodeGLSupplier> list = new ArrayList<PerekamanKodeGLSupplier>();
        List<Map> rows = ClassConnection.getJdbc().queryForList("SELECT * FROM kamus.suplier");
        for(Map m : rows){
            PerekamanKodeGLSupplier supplier = new PerekamanKodeGLSupplier();
            supplier.setKodeSupplier(Integer.valueOf(m.get("kodeSuplier").toString()));
            supplier.setNamaSupplier(m.get("namaSuplier").toString());
            supplier.setAlamat(m.get("alamat").toString());
            supplier.setTelp(m.get("telp").toString());
            supplier.setKodeGLSupplier(ClassAntiNull.AntiNullString(m.get("kodeGLSuplier")));
            list.add(supplier);
        }
        return list;
    }

    public List<PerekamanKodeGLSupplier> list(String key) throws Exception {
        List<PerekamanKodeGLSupplier> list = new ArrayList<PerekamanKodeGLSupplier>();
        List<Map> rows = ClassConnection.getJdbc().queryForList("SELECT * FROM kamus.suplier WHERE kodeGLSuplier LIKE ? OR namaSuplier LIKE ?",
                new Object[]{
            "%"+key+"%",
            "%"+key+"%"
        });
        for(Map m : rows){
            PerekamanKodeGLSupplier supplier = new PerekamanKodeGLSupplier();
            supplier.setKodeSupplier(Integer.valueOf(m.get("kodeSuplier").toString()));
            supplier.setNamaSupplier(m.get("namaSuplier").toString());
            supplier.setAlamat(m.get("alamat").toString());
            supplier.setTelp(m.get("telp").toString());
            supplier.setKodeGLSupplier(ClassAntiNull.AntiNullString(m.get("kodeGLSuplier")));
            list.add(supplier);
        }
        return list;
    }

    public void update(PerekamanKodeGLSupplier supplier) throws Exception {
        String sql = "UPDATE kamus.suplier SET kodeGLSuplier=? WHERE kodeSuplier=?";
        ClassConnection.getJdbc().update(sql, new Object[]{ClassUtility.quote(supplier.getKodeGLSupplier()),
                    Integer.valueOf(supplier.getKodeSupplier())});
    }

    public List select(int kodeSupplier, String kodeGL) throws Exception {
        String sql = "SELECT * FROM kamus.suplier WHERE kodeSuplier<>? AND kodeGLSuplier=?";
        return ClassConnection.getJdbc().queryForList(sql,new Object[]{
            Integer.valueOf(kodeSupplier),
            kodeGL
        });
    }
}
